<?
session_start();
header('Content-type: text/html;charset=utf-8');

include_once('../funcs.php');
include_once('../db_info.php');

if ($_SESSION['manager_login']!=true)
{
	header ("Location: index.php",true,301);
	//print_r($_SESSION);
    exit();
};


//GET MANAGER_ROW
$sql="select * from managers where id=".$_SESSION['manager_id']." ";
if(!$result = $GLOBALS['mysqli_conn1']->query($sql))
{
	echo "<br>".$sql;
	exit();
};
	
if (mysqli_num_rows($result)==0)
{
	header ("Location: index.php",true,301);
    exit();
};

$manager_row=mysqli_fetch_array($result);


if (isset($_POST['date_from']))
{
	$date_from=$_POST['date_from'];
}
else
{
	$date_from=date("Y.m.d",time()-60*60*24*30*3);
};

if (isset($_POST['date_till']))
{
	$date_till=$_POST['date_till'];
}
else
{
	$date_till=date("Y.m.d");
};

$date_till_mass=explode(".",$date_till);
$date_from_mass=explode(".",$date_from);

$dop_orders=" and create_date>='".implode("",$date_from_mass)."' and create_date<='".implode("",$date_till_mass)."' ";
$dop_visti_tbl=" and date>='".implode("",$date_from_mass)."' and date <='".implode("",$date_till_mass)."' ";
$dop_partners=" and create_date>='".implode("",$date_from_mass)."' and create_date <='".implode("",$date_till_mass)."' ";


$status_mas[1]="Новая";
$status_mas[2]="Идет расчет";
$status_mas[3]="Перезвонить";
$status_mas[4]="Отказ";
$status_mas[5]="Неверный номер";
$status_mas[6]="Отправлено на реализацию";
$status_mas[7]="Оплачено";

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"/>
    <title>kz-pro.com - Менеджер</title>
    <link type="text/css" rel="stylesheet" href="style.css"/>
	<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
	<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    <!--[if lte IE 7]><link type="text/css" rel="stylesheet" href="ie.css" /><![endif]-->
</head>

<body>
    <div class="insidepage">
        <div id="page">
            <div id="header">
                <h1 class="logo"><a href="#" title="kz-pro.com - Партнерская программа автострахования">kz-pro.com - Менеджер</a></h1>
                <ul class="mainmenu">
                    <li class="active"><span><em>Главная</em></span></li>
                    <li class="logout"><span><a href="exit.php" title="Выход">Выход</a></span></li>
                </ul>
                <div class="userinfo">Вы вошли как, <a href="#" title="Wildcat"><?echo $manager_row['login'];?></a> <? echo (strlen($_SESSION['role_name'])) ? "(${_SESSION['role_name']})" : '' ?> </div>

            </div>
            <div id="content">
                <div class="clearfix">
                    <form action="" method="post" class="filterForm">
                        <label for="filterForm_from">Показать заявки с: </label>
                        <span class="input_text"><span><span><input name="date_from" id="filterForm_from" type="text" value="<?echo $date_from;?>" /></span></span></span>
                        <label for="filterForm_till">по: </label>
                        <span class="input_text"><span><span><input name="date_till" id="filterForm_till" type="text" value="<?echo $date_till;?>" /></span></span></span>
                        <span class="blueBtn"><span><span><input type="submit" value="Показать" /></span></span></span>
                    </form>
                </div>
                <div class="table_title">
                    <h3><span>Заявки</span> по дате создания</h3>
                    <br /><br />
                </div>
                <table class="table">
				<?
				if($_SESSION['superviser'] == 0)
				{
				?>
                    <thead>
                        <tr>
                            <th>Номер заявки</th>
							<th>Дата создания</th>
							<th>Город</th>
							<th>ФИО</th>
                            <th>Телефон</th>
                            <th>Статус</th>
							<th>Информация по машине</th>
							<th>Страховая и стоимость</th>
                            <th>Комментарии</th>
                            <th>Обработать</th>
                        </tr>
                    </thead>
				<?
					$rows = array();
					$t    = "select * from orders where manager_id=".$_SESSION['manager_id']." ".$dop_orders." %s order by create_time desc ";
				
					$sql = sprintf($t, 'and orders.`status` = 1');
					
					$result = queryWithExit($GLOBALS['mysqli_conn1'], $sql);				
				
					while ($row = mysqli_fetch_array($result))
						$rows[] = $row;

					$sql    = sprintf($t, 'and orders.`status` <> 1');
					
					$result = queryWithExit($GLOBALS['mysqli_conn1'], $sql);
					
					while ($row = mysqli_fetch_array($result))
						$rows[] = $row;
					
					foreach($rows as $row)
					{
						$status=$status_mas[$row['status']];
						if ($row['status']==3)
							$status.=" <br />".$row['new_ring_date'];
						
						switch($row['city'])
						{
							case "msc":
								$city="Москва";
							break;
							default:
								$city=$row['city'];
								
						};
						
						$sk_info_str='нет расчета';
						if ($row['realCost']<>0)
							$sk_info_str=$row['skName'].' - '.$row['realCost'].'руб';
						
						echo '
						<tr>
							<td>'.$row['order_id'].'</td>
							<td>'.date('Y-m-d H:i',$row['create_time']).'</td>
							<td>'.$city.'</td>
							<td>'.$row['fio'].'</td>
							<td>'.$row['tel1'].'</td>
							<td>'.$status.'</td>
							<td>'.$row['manufacturerName'].' - '.$row['carName'].'</td>
							<td>'.$sk_info_str.'</td>
							<td>'.$row['comments'].'</td>
							<td class="actions">
								<a href="orders.php?order_id='.$row['order_id'].'" title="Обработка" class="edit">На страницу заявки</a>
							</td>
						</tr>';
					};
				}
				else
				{
					?>
                    <thead>
                        <tr>
                            <th>Номер заявки</th>
							<th>Менеджер</th>                                       
							<th>Дата создания</th>                                  
							<th>Город</th>                                          
							<th>ФИО</th>                                            
                            <th>Телефон</th>                   
                            <th>Статус</th>                    
							<th>Информация по машине</th>                           
							<th>Страховая и стоимость</th>                          
                            <th>Комментарии</th>               
                            <th>Обработать</th>
                        </tr>
                    </thead>
				<?
					$rows = array();
					$t="select *,managers.fio as mfio,orders.fio as ofio from orders,managers where managers.id=orders.manager_id and admin=0 ".$dop_orders." %s order by create_time desc ";

					$sql = sprintf($t, 'and orders.`status` = 1');
					
					$result = queryWithExit($GLOBALS['mysqli_conn1'], $sql);				
				
					while ($row = mysqli_fetch_array($result))
						$rows[] = $row;

					$sql    = sprintf($t, 'and orders.`status` <> 1');
					
					$result = queryWithExit($GLOBALS['mysqli_conn1'], $sql);
					
					while ($row = mysqli_fetch_array($result))
						$rows[] = $row;
					
					foreach($rows as $row)
					{
						$status=$status_mas[$row['status']];
						if ($row['status']==3)
							$status.=" <br />".$row['new_ring_date'];
						
						switch($row['city'])
						{
							case "msc":
								$city="Москва";
							break;
							default:
								$city="Неизвестно";
								
						};
						
						$sk_info_str='нет расчета';
						if ($row['realCost']<>0)
							$sk_info_str=$row['skName'].' - '.$row['realCost'].'руб';
						
						
						echo '
						<tr>
							<td>'.$row['order_id'].'</td>
							<td>'.$row['mfio'].'</td>
							<td>'.date('Y-m-d H:i',$row['create_time']).'</td>
							<td>'.$city.'</td>
							<td>'.$row['ofio'].'</td>
							<td>'.$row['tel1'].'</td>
							<td>'.$status.'</td>
							<td>'.$row['manufacturerName'].' - '.$row['carName'].'</td>
							<td>'.$sk_info_str.'</td>
							<td>'.$row['comments'].'</td>
							<td class="actions">
								<a href="orders.php?order_id='.$row['order_id'].'" title="Обработка" class="edit">На страницу заявки</a>
							</td>
						</tr>';
					};
				
				};
				?>
				
                </table>
                <div class="table_title">
                      <h3><span>Заявки</span> по времени перезвона.<h3>
                    <br /><br />

                </div>
                <table class="table">
				<?
				if($_SESSION['superviser']==0)
				{
				?>
                    <thead>
                        <tr>
                            <th>Номер заявки</th>
							<th>Дата создания</th>                                
							<th>Город</th>                                        
							<th>ФИО</th>                                          
                            <th>Телефон</th>                 
                            <th>Статус</th>                  
							<th>Информация по машине</th>                         
                            <th>Комментарии</th>             
                            <th>Обработать</th>              
                        </tr>
                    </thead>
					<?
					$sql="select * from orders where manager_id=".$_SESSION['manager_id']." and status=3 and new_ring_date<>'' ".$dop_orders." order by new_ring_date ";
					if(!$result = $GLOBALS['mysqli_conn1']->query($sql))
					{
						echo "<br>".$sql;
						exit();
					};
					
					while ($row=mysqli_fetch_array($result))
					{
						
						$status=$status_mas[$row['status']];
						
						if ($row['status']==3)
							$status.=" <br />".$row['new_ring_date'];
						
						switch($row['city'])
						{
							case "msc":
								$city="Москва";
							break;
							default:
								$city=$row['city'];
								
						};
						
						echo '
						<tr>
							<td>'.$row['order_id'].'</td>
							<td>'.date('Y-m-d H:i',$row['create_time']).'</td>
							<td>'.$city.'</td>
							<td>'.$row['fio'].'</td>
							<td>'.$row['tel1'].'</td>
							<td>'.$status.'</td>
							<td>'.$row['manufacturerName'].' - '.$row['carName'].'</td>
							<td>'.$row['comments'].'</td>
							<td class="actions">
								<a href="orders.php?order_id='.$row['order_id'].'" title="Обработка" class="edit">На страницу заявки</a>
							</td>
						</tr>';
					};
				}
				else
				{
					?>
                    <thead>
                        <tr>
                            <th>Номер заявки</th>
							<th>Менеджер</th>                                       
							<th>Дата создания</th>
							<th>Город</th>
							<th>ФИО</th>
                            <th>Телефон</th>
                            <th>Статус</th>
							<th>Информация по машине</th>
                            <th>Комментарии</th>
                            <th>Обработать</th>
                        </tr>
                    </thead>
					<?
					$sql="select *, managers.fio as mfio, orders.fio as ofio from orders, managers where managers.id=orders.manager_id and admin=0 and status=3 and new_ring_date<>'' ".$dop_orders." order by new_ring_date ";
					if(!$result = $GLOBALS['mysqli_conn1']->query($sql))
					{
						echo "<br>".$sql;
						exit();
					};
					
					while ($row=mysqli_fetch_array($result))
					{
						
						$status=$status_mas[$row['status']];
						if ($row['status']==3)
							$status.=" <br />".$row['new_ring_date'];
						
						switch($row['city'])
						{
							case "msc":
								$city="Москва";
							break;
							default:
								$city=$row['city'];
								
						};
						
						echo '
						<tr>
							<td>'.$row['order_id'].'</td>
							<td>'.$row['mfio'].'</td>
							<td>'.date('Y-m-d H:i',$row['create_time']).'</td>
							<td>'.$city.'</td>
							<td>'.$row['ofio'].'</td>
							<td>'.$row['tel1'].'</td>
							<td>'.$status.'</td>
							<td>'.$row['manufacturerName'].' - '.$row['carName'].'</td>
							<td>'.$row['comments'].'</td>
							<td class="actions">
								<a href="orders.php?order_id='.$row['order_id'].'" title="Обработка" class="edit">На страницу заявки</a>
							</td>
						</tr>';
					};
				};
				?>
                </table>
            </div>
        </div>
        <div id="footer_out"><div id="footer">Все права защищены компанией "Kz-pro"</div></div>
    </div>
	<script type="text/javascript">
		function locs(){
			document.location.href="lk.php";
		}
		setTimeout("locs()", 300000);
	</script>
</body>
</html>
